package com.ourteam.frame.web;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.dao.BaseQueryBean;

import org.apache.commons.lang.StringUtils;

public abstract class AbstractSearchHelper<T extends BaseQueryBean, S extends AbstractSearchModel<T>> {

	private S searchModel;

	public AbstractSearchHelper(S searchModel) {
		super();
		this.searchModel = searchModel;
	}

	public AbstractSearchHelper() {
		super();
	}
	
	abstract protected int queryCount(T queryBean) throws Exception;

	abstract protected Object[] queryData(T queryBean) throws Exception;

	abstract protected T createQueryBean();

	protected String getRowIdProperty() {
		return "id";
	}

	public GridBean doSearch(HttpServletRequest request) throws Exception {

		int columnCount = StringUtils.isNotBlank(request.getParameter("iColumns"))
				? Integer.parseInt(request.getParameter("iColumns")) : 0;

		if (columnCount > 0) {

			String[] dataProperties = new String[columnCount];

			for (int i = 0; i < columnCount; i++) {
				dataProperties[i] = request.getParameter("mDataProp_" + i);
			}

			int sortColumnCount = StringUtils.isNotBlank(request.getParameter("iSortingCols"))
					? Integer.parseInt(request.getParameter("iSortingCols")) : 0;

			String[] sortProperties = new String[sortColumnCount];

			String[] sortDirs = new String[sortColumnCount];

			for (int i = 0; i < sortColumnCount; i++) {
				sortProperties[i] = dataProperties[Integer.parseInt(request.getParameter("iSortCol_" + i))];
				sortDirs[i] = request.getParameter("sSortDir_" + i);
			}
			
			searchModel.setSortProperties(sortProperties);

			searchModel.setSortDirs(sortDirs);
		}
		else{
			Enumeration<String> parameterNames = request.getParameterNames();
			
			List<String> sortProperties = new ArrayList<String>();
			
			List<String> sortDirs = new ArrayList<String>();
			
			while (parameterNames.hasMoreElements()) {
				String parameterName = parameterNames.nextElement();
				if (parameterName.startsWith("sort[") && parameterName.endsWith("[direction]")) {
					sortDirs.add(request.getParameter(parameterName));
				}
				if(parameterName.startsWith("sort[") && parameterName.endsWith("[field]")){
					sortProperties.add(request.getParameter(parameterName));
				}
				if(parameterName.equals("sortField") ) {
					sortProperties.add(request.getParameter(parameterName));
				}
				if(parameterName.equals("sortOrder") ) {
					if("ascend".equals(request.getParameter(parameterName))) {
						sortDirs.add("asc");
					}
					else {
						sortDirs.add("desc");
					}
					
				}
			}
			if(sortProperties.isEmpty() == false){
				searchModel.setSortProperties(sortProperties.toArray(new String[sortProperties.size()]));

				searchModel.setSortDirs(sortDirs.toArray(new String[sortDirs.size()]));
			}
			
		}

		GridBean grid = new GridBean();

		grid.setRowIdProperty(getRowIdProperty());

		grid.setPage(searchModel.getPage());

		grid.setPageCount(searchModel.getRows());

		grid.setsEcho(searchModel.getsEcho());

		T queryBean = createQueryBean();

		searchModel.buildQueryBean(queryBean);

		int count = this.queryCount(queryBean);

		grid.setRecords(count);

		queryBean.clear();

		searchModel.buildPageQueryBean(queryBean);

		grid.setRows(this.queryData(queryBean));

		return grid;
	}

	public S getSearchModel() {
		return searchModel;
	}

	public void setSearchModel(S searchModel) {
		this.searchModel = searchModel;
	}

}
